/*

Author   : chaos

FileName : binary_mode.cpp

Date     : 16/08/17 11:39:52

*/
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

#define FILEPATH "Reader.txt"

int main(int argc ,char *argv[])
{
    fstream file;
    char str[4][8] = {"Mary","John","Judy","Joe"};
    int id[4] = {100,200,300,400};

    file.open(FILEPATH,ios::out | ios::binary | ios::trunc);

    if(!file)
    {
        cerr << "Can't open file!" << endl;
        exit(1);
    }

    for(int i; i < 4; i++)
    {
        file.write((char*) &id[i],sizeof(int));
        file.write(str[i],8*sizeof(char));
    }

    char str1[8];
    int id1;

    file.close();
    file.open(FILEPATH,ios::in | ios::binary);

    if(!file)
    {
        cerr << "Can't open file!" << endl;

    }
    cout << setw(4) << setiosflags(ios::right) << "ID"
        <<setw(8) << setiosflags(ios::right) << "Name" << endl;

    cout << "---------------------------------" << endl;

    file.read((char*) &id1,sizeof(int));
    file.read(str1,8 * sizeof(char));

    while(!file.eof())
    {
        cout << setw(4) << setiosflags(ios::right) << id1
            << setw(8) << setiosflags(ios::right) << str1 << endl;
        file.read((char*) & id1, sizeof(int));
        file.read((char*) str1, sizeof(char));
    }
    return 0;
}
